<?php
namespace Admin\Controller;
use Admin\Controller\AdminBaseController;
use Common\Model\UserMoneyModel;
use Common\Model\CurrencyModel;
use Common\Model\RechargeLogModel;
use Common\Model\WithdrawalsModel;
class UserMoneyController extends AdminBaseController{
	public function moneyInfo()
	{
        $username=I('username'); 
        $user=$this->findUserByUserName($username);
	    
	    $money_info=UserMoneyModel::newSelf()->getUserMoneyInfo($user['id']);
	    
        $currency=CurrencyModel::newSelf()->getInfo();
        
        
        $this->assign('currency',$currency);
	    $this->assign('info',$user);
	    $this->assign('money_info',$money_info);
	    $this->display();
	}
	public function changeMoneyFormSub()
	{
	    $input=I('post.');
	    
	    $data['user_id']=I('user_id');
	    $data['type']=I('type');
	    $data['num']=I('num');
	    $data['currency_id']=I('currency_id');
        $this->notEmptyValidate($data['user_id'],'用户不存在');
        $this->notEmptyValidate($data['type'],'请选择类型');
        $this->notEmptyValidate($data['num'],'请填写金额');
        $this->numVerification($data['num'],'请输入正确金额');
        $this->gtValidate($data['num'],0,'请输入正确金额');
        $this->notEmptyValidate($data['currency_id'],'请选择币种');
        
        
        $user=$this->findUserByUserId($data['user_id']);
        $this->notEmptyValidate($user,'用户不存在');
        
        $currency=CurrencyModel::newSelf()->findOneById($data['currency_id']);
        $this->notEmptyValidate($currency,'币种不存在');
        
        $model=UserMoneyModel::newSelf();
        
        if ($data['type']=='inc') {
            $res=$model->incUserMoney($data['user_id'],$data['currency_id'],$data['num']);
        }
        
        if ($data['type']=='dec') {
            $user_money=UserMoneyModel::newSelf()->getUserMoneyInfo($data['user_id']);
            $this->egtValidate($user_money[$data['currency_id']]['num'],$data['num'],'该用户的'.$currency['name'].'不足');
            $res=$model->decUserMoney($data['user_id'],$data['currency_id'],$data['num']);
        }
        $data['add_time']=time();
        $data['admin_id']=$_SESSION['sys_user']['userid'];
        RechargeLogModel::newSelf()->add($data);
        $this->resReturn($res, '操作成功', '操作失败');
	}
	public function withdrawalsLog()
	{
		$list=WithdrawalsModel::newSelf()->pagingList();
		$this->assign('list',$list);
		$this->display();
	}
	public function withdrawalsYes()
	{
		$id=I('id');
		$this->notEmptyValidate($id,'请正确操作');
		
		
		$model=WithdrawalsModel::newSelf();
		$info=$model->findOneById($id);
		
		$this->eqValidate($info['status'],'0','该条记录已经审核过');
		$data['active_time']=time();
		$data['status']=1;
		$res=$model->where(['id'=>$id])->save($data);
		$this->resReturn($res,'审核成功','审核失败'); 
	}
	public function withdrawalsNo()
	{
		$id=I('id');
		$this->notEmptyValidate($id,'请正确操作');
		
		
		$model=WithdrawalsModel::newSelf();
		$info=$model->findOneById($id);
		
		$this->eqValidate($info['status'],'0','该条记录已经审核过');
		$data['active_time']=time();
		$data['status']=-1;
		
		UserMoneyModel::newSelf()->incUserMoney($info['user_id'], $info['currency_id'], $info['num']);
        $res = $model->where(['id'=>$id])->save($data);
		$this->resReturn($res,'审核成功','审核失败'); 
	}
}
